<template>
  <!-- 全局加载遮罩 -->
  <div 
    v-if="visible" 
    class="loading-overlay"
    :class="overlayClass"
    @click="handleOverlayClick"
  >
    <div class="loading-content" @click.stop>
      <!-- 加载动画 -->
      <div class="loading-spinner" :class="spinnerClass">
        <!-- 默认加载动画 -->
        <div v-if="type === 'default'" class="spinner-default">
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
        </div>
        
        <!-- 点状加载动画 -->
        <div v-else-if="type === 'dots'" class="spinner-dots">
          <div class="dot"></div>
          <div class="dot"></div>
          <div class="dot"></div>
        </div>
        
        <!-- 脉冲加载动画 -->
        <div v-else-if="type === 'pulse'" class="spinner-pulse">
          <div class="pulse-circle"></div>
        </div>
        
        <!-- 波浪加载动画 -->
        <div v-else-if="type === 'wave'" class="spinner-wave">
          <div class="wave-bar"></div>
          <div class="wave-bar"></div>
          <div class="wave-bar"></div>
          <div class="wave-bar"></div>
          <div class="wave-bar"></div>
        </div>
        
        <!-- 旋转方块动画 -->
        <div v-else-if="type === 'cube'" class="spinner-cube">
          <div class="cube"></div>
          <div class="cube"></div>
          <div class="cube"></div>
          <div class="cube"></div>
        </div>
        
        <!-- Element Plus 加载动画 -->
        <el-icon v-else-if="type === 'element'" class="spinner-element">
          <Loading />
        </el-icon>
        
        <!-- 自定义图标 -->
        <div v-else-if="type === 'custom' && customIcon" class="spinner-custom">
          <i :class="customIcon"></i>
        </div>
      </div>
      
      <!-- 加载文本 -->
      <div v-if="text" class="loading-text" :style="textStyle">
        {{ text }}
      </div>
      
      <!-- 进度条 -->
      <div v-if="showProgress" class="loading-progress">
        <el-progress 
          :percentage="progress" 
          :show-text="showProgressText"
          :stroke-width="progressStrokeWidth"
          :color="progressColor"
        />
      </div>
      
      <!-- 取消按钮 -->
      <el-button 
        v-if="showCancel" 
        @click="handleCancel"
        class="loading-cancel"
        size="small"
        type="info"
        plain
      >
        {{ cancelText }}
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { Loading } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  // 是否显示
  visible: {
    type: Boolean,
    default: false
  },
  // 加载动画类型
  type: {
    type: String,
    default: 'default',
    validator: (value) => {
      return ['default', 'dots', 'pulse', 'wave', 'cube', 'element', 'custom'].includes(value)
    }
  },
  // 加载文本
  text: {
    type: String,
    default: ''
  },
  // 文本颜色
  textColor: {
    type: String,
    default: ''
  },
  // 文本大小
  textSize: {
    type: String,
    default: '14px'
  },
  // 加载动画大小
  size: {
    type: String,
    default: 'medium',
    validator: (value) => {
      return ['small', 'medium', 'large'].includes(value)
    }
  },
  // 加载动画颜色
  color: {
    type: String,
    default: ''
  },
  // 背景颜色
  background: {
    type: String,
    default: ''
  },
  // 是否显示进度条
  showProgress: {
    type: Boolean,
    default: false
  },
  // 进度值
  progress: {
    type: Number,
    default: 0
  },
  // 是否显示进度文本
  showProgressText: {
    type: Boolean,
    default: true
  },
  // 进度条粗细
  progressStrokeWidth: {
    type: Number,
    default: 6
  },
  // 进度条颜色
  progressColor: {
    type: String,
    default: ''
  },
  // 是否显示取消按钮
  showCancel: {
    type: Boolean,
    default: false
  },
  // 取消按钮文本
  cancelText: {
    type: String,
    default: '取消'
  },
  // 自定义图标类名
  customIcon: {
    type: String,
    default: ''
  },
  // 是否可以点击遮罩关闭
  closeOnClickModal: {
    type: Boolean,
    default: false
  },
  // z-index
  zIndex: {
    type: Number,
    default: 2000
  },
  // 是否全屏
  fullscreen: {
    type: Boolean,
    default: true
  }
})

// Emits
const emit = defineEmits(['cancel', 'close'])

// 计算属性
const overlayClass = computed(() => ({
  'is-fullscreen': props.fullscreen,
  [`loading-${props.size}`]: true
}))

const spinnerClass = computed(() => ({
  [`spinner-${props.size}`]: true
}))

const textStyle = computed(() => ({
  color: props.textColor || 'var(--el-text-color-primary)',
  fontSize: props.textSize
}))

// 方法
const handleOverlayClick = () => {
  if (props.closeOnClickModal) {
    emit('close')
  }
}

const handleCancel = () => {
  emit('cancel')
}
</script>

<style lang="scss" scoped>
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: v-bind(zIndex);
  display: flex;
  align-items: center;
  justify-content: center;
  background: v-bind(background) || rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
  
  &.is-fullscreen {
    position: fixed;
  }
  
  .loading-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    border-radius: 8px;
    background: var(--el-bg-color);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    
    .loading-spinner {
      margin-bottom: 16px;
      
      &.spinner-small {
        transform: scale(0.8);
      }
      
      &.spinner-large {
        transform: scale(1.2);
      }
    }
    
    .loading-text {
      margin-bottom: 16px;
      font-weight: 500;
      text-align: center;
      white-space: nowrap;
    }
    
    .loading-progress {
      width: 200px;
      margin-bottom: 16px;
    }
    
    .loading-cancel {
      margin-top: 8px;
    }
  }
}

// 默认加载动画
.spinner-default {
  position: relative;
  width: 40px;
  height: 40px;
  
  .spinner-ring {
    position: absolute;
    width: 32px;
    height: 32px;
    margin: 4px;
    border: 3px solid transparent;
    border-top-color: v-bind(color) || var(--el-color-primary);
    border-radius: 50%;
    animation: spin 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
    
    &:nth-child(1) {
      animation-delay: -0.45s;
    }
    
    &:nth-child(2) {
      animation-delay: -0.3s;
    }
    
    &:nth-child(3) {
      animation-delay: -0.15s;
    }
  }
}

// 点状加载动画
.spinner-dots {
  display: flex;
  gap: 4px;
  
  .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: v-bind(color) || var(--el-color-primary);
    animation: dot-bounce 1.4s ease-in-out infinite both;
    
    &:nth-child(1) {
      animation-delay: -0.32s;
    }
    
    &:nth-child(2) {
      animation-delay: -0.16s;
    }
  }
}

// 脉冲加载动画
.spinner-pulse {
  .pulse-circle {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: v-bind(color) || var(--el-color-primary);
    animation: pulse 1.5s ease-in-out infinite;
  }
}

// 波浪加载动画
.spinner-wave {
  display: flex;
  gap: 2px;
  align-items: center;
  
  .wave-bar {
    width: 4px;
    height: 20px;
    background: v-bind(color) || var(--el-color-primary);
    border-radius: 2px;
    animation: wave 1.2s ease-in-out infinite;
    
    &:nth-child(1) {
      animation-delay: -1.1s;
    }
    
    &:nth-child(2) {
      animation-delay: -1.0s;
    }
    
    &:nth-child(3) {
      animation-delay: -0.9s;
    }
    
    &:nth-child(4) {
      animation-delay: -0.8s;
    }
    
    &:nth-child(5) {
      animation-delay: -0.7s;
    }
  }
}

// 旋转方块动画
.spinner-cube {
  position: relative;
  width: 40px;
  height: 40px;
  
  .cube {
    position: absolute;
    width: 16px;
    height: 16px;
    background: v-bind(color) || var(--el-color-primary);
    border-radius: 2px;
    animation: cube-rotate 1.8s infinite ease-in-out;
    
    &:nth-child(1) {
      top: 0;
      left: 0;
      animation-delay: -0.9s;
    }
    
    &:nth-child(2) {
      top: 0;
      right: 0;
      animation-delay: -0.6s;
    }
    
    &:nth-child(3) {
      bottom: 0;
      left: 0;
      animation-delay: -0.3s;
    }
    
    &:nth-child(4) {
      bottom: 0;
      right: 0;
    }
  }
}

// Element Plus 加载动画
.spinner-element {
  font-size: 32px;
  color: v-bind(color) || var(--el-color-primary);
  animation: spin 1s linear infinite;
}

// 自定义图标动画
.spinner-custom {
  i {
    font-size: 32px;
    color: v-bind(color) || var(--el-color-primary);
    animation: spin 1s linear infinite;
  }
}

// 动画定义
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes dot-bounce {
  0%, 80%, 100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

@keyframes pulse {
  0% {
    transform: scale(0);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

@keyframes wave {
  0%, 40%, 100% {
    transform: scaleY(0.4);
  }
  20% {
    transform: scaleY(1);
  }
}

@keyframes cube-rotate {
  0%, 70%, 100% {
    transform: scale3D(1, 1, 1);
  }
  35% {
    transform: scale3D(0, 0, 1);
  }
}

// 深色模式适配
@media (prefers-color-scheme: dark) {
  .loading-overlay {
    background: rgba(0, 0, 0, 0.8);
  }
}
</style>